package com.widevision.adapter;

import java.util.ArrayList;

import com.androidquery.AQuery;
import com.widevision.ListActivity.UserProfile;

import net.widevision.android.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TweetAdapter extends BaseAdapter{
	
	//***********************************//
    private AQuery mainAQuery;
    //***********************************//
	ArrayList<Object> itemList;
 
    public Activity context;
    public LayoutInflater inflater;
    private int selectedPos= -1;
    static int h=0;
 
    public TweetAdapter(Activity context,ArrayList<Object> itemList,AQuery aq) {
        super();
 
        this.context = context;
        this.itemList = itemList;
        
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //***********************************//
        mainAQuery = aq;
    }
    
    public void setSelectedPosition(int pos){
		selectedPos = pos;
		// inform the view of this change
		notifyDataSetChanged();
	}
    
    public int getSelectedPosition(){
		return selectedPos;
	}
    
    public int getCount() {
       
        return itemList.size();
    }
 
    
    public Object getItem(int position) {
       
        return itemList.get(position);
    }
 
  
    public long getItemId(int position) {
        
        return 0;
    }
 
    public static class ViewHolder
    {
        ImageView profileimg,minitwimg,timeimg;
        TextView screenName,userName,tweet,time,timeago,idvalue;
        LinearLayout optionlayout,tweetlayout,username;
    }
 
 
    public View getView(int position, View convertView, ViewGroup parent) 
    {
      
    	ViewHolder holder= new ViewHolder();
        if(convertView==null)
        {
           
            convertView = inflater.inflate(R.layout.tweetitemlist, null);
            
            holder.profileimg  = (ImageView) convertView.findViewById(R.id.profileimg);
            holder.minitwimg   = (ImageView) convertView.findViewById(R.id.timeimg);
            holder.timeimg     = (ImageView) convertView.findViewById(R.id.minitwimg);
            holder.screenName  = (TextView) convertView.findViewById(R.id.screenName);
            holder.tweet       = (TextView) convertView.findViewById(R.id.tweettext);
            holder.userName    = (TextView) convertView.findViewById(R.id.username);
            holder.time        = (TextView) convertView.findViewById(R.id.time);
            holder.username    = (LinearLayout)convertView.findViewById(R.id.usernamee);
            
            holder.optionlayout=(LinearLayout) convertView.findViewById(R.id.optionLayout);	
            holder.tweetlayout =(LinearLayout) convertView.findViewById(R.id.tweetoption);
            convertView.setTag(holder);
            
        }
        else
        	
        {
        holder=(ViewHolder)convertView.getTag();
 
        TweetBean bean = (TweetBean) itemList.get(position);
        //***********************************//
        AQuery aq = mainAQuery.recycle(convertView);
        //***********************************//
        aq.id(R.id.profileimg).image(bean.getProfileimg(), true, true, 0, 0, null, AQuery.FADE_IN, AQuery.RATIO_PRESERVE);
        aq.id(R.id.minitwimg).image(bean.getMinitwimg(), true, true, 0, 0, null, AQuery.FADE_IN, AQuery.RATIO_PRESERVE);
        aq.id(R.id.timeimg).image(bean.getTimeimg(), true, true, 0, 0, null, AQuery.FADE_IN, AQuery.RATIO_PRESERVE);
//        holder.profileimg.setBackgroundDrawable(bean.getProfileimg());
//        holder.minitwimg.setBackgroundDrawable(bean.getMinitwimg());
//        holder.timeimg.setBackgroundDrawable(bean.getTimeimg());
        holder.screenName.setText(bean.getScreenName());
        holder.tweet.setText(bean.getTweet());
        holder.userName.setText(bean.getUserName());
        holder.time.setText(bean.getTime());}
        
//        if(selectedPos == position){
//        	if(h==0)
//        	{
//        	holder.optionlayout.setVisibility(View.VISIBLE);
//        	 Log.d("if>>>>>>>>", ""+h);
//        	 h=1;
//        	}
//        	else if(h==1)
//        	{
//        	holder.optionlayout.setVisibility(View.GONE);
//        	 Log.d("else>>>>>>>>", ""+h);
//        	h=0;
//        	}
//        	Log.d("position>>>>>>>>", ""+position);
//            	
//        }
//        else{
//        	holder.optionlayout.setVisibility(View.GONE);
//        	Log.d("position>>>>>>>>", ""+position);
//        	
//        }
//    
//        if(selectedPos == position)
//        {
//        	holder.optionlayout.setVisibility(View.VISIBLE);
//        }
//        else
//        	holder.optionlayout.setVisibility(View.GONE);
        
//        holder.username.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) 
//			{
//				
//				Intent i=new Intent(context,UserProfile.class);
//				String idvalue=holder.idvalue.getText();
//				int userId=Integer.parseInt(idvalue);
//				i.putExtra("USERID", userId);
//				context.startActivity(i);
//			}
//			});
        convertView.setTag(holder);
        return convertView;
        
    }
 
}

